//
//  Items.m
//  HYHOME
//
//  Created by Mac on 16/1/22.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "Items.h"
#import "CellForpicUrlCollectionViewCell.h"
#import "ModelImage.h"
#import "UIImageView+WebCache.h"
#import "CellForKT.h"
#import "CellForCT.h"
#import "WSJ.h"
#import "Other.h"
#import "ShowItem.h"

#import "DKNightVersion.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

#define WIDTH1 [UIScreen mainScreen].bounds.size.width
#define HEIGHT1 [UIScreen mainScreen].bounds.size.height
@interface Items ()<UICollectionViewDelegate,UICollectionViewDataSource,NSURLSessionDataDelegate,NSURLSessionDelegate,NSURLSessionTaskDelegate, CellForpicUrlCollectionViewCellDelegate,CellForKTDelegate,CellForCTDelegate,WSJDelegate,OtherDelegate>

@property (nonatomic,retain)UICollectionView *collectView;
@property (nonatomic,retain)UICollectionViewFlowLayout *flowLayout;

@property (nonatomic,retain)UIScrollView *scrollView;
@property (nonatomic,retain)UIView *viewForRedline;

// scrollView 的arr
@property (nonatomic,retain)NSMutableArray *arrayTitle;

// 用来接收数据
@property (nonatomic,retain)NSMutableData *data;

@property (nonatomic,retain)NSMutableArray *arr;

@property (nonatomic,retain)NSMutableArray *arrPic;

@property (nonatomic,retain)NSMutableArray *arrkt;
@end

@implementation Items



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
  
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.title = @"分类";
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
 
    
    
    
    
    [self createCollectionView];
    [self createKVOAction];
    [self createScrollView];


}


- (void)createScrollView{

     self.arrayTitle = @[@"卧室",@"客厅",@"餐厅", @"浴室",@"其他"].mutableCopy;

    
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 40)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = YES;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    self.scrollView.dk_backgroundColorPicker= DKColorWithRGB(0xffffff, 0x343434);

    [self.view addSubview:self.scrollView];
    
    self.scrollView.contentSize = CGSizeMake(WIDTH / 5 * self.arrayTitle.count, 0);

    
    [self.scrollView release];
    
    for (int i = 0 ; i<self.arrayTitle.count; i++) {
        
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:self.arrayTitle[i] forState:UIControlStateNormal];
        
        button.frame = CGRectMake(WIDTH / 5 *  i , 10, WIDTH / 5 -10, 30);
        button.tag = i+1;
        
        [button addTarget:self action:@selector(handleOfbutton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView addSubview:button];
    
        
    }

    [self createRedLine];

    
}



- (void)createRedLine{
    
    self.viewForRedline = [[UIView alloc]initWithFrame:CGRectMake(0,100, WIDTH/5-10, 2)];
    
    self.viewForRedline.backgroundColor = [UIColor cyanColor];
    self.viewForRedline.alpha = 0.3;
    [self.view addSubview:self.viewForRedline];
    
    [self.viewForRedline release];
}

- (void)handleOfbutton:(UIButton *)button{

    
        
        NSIndexPath *path = [NSIndexPath indexPathForItem:button.tag - 1 inSection:0];
    
        [self.collectView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    
    


}


#pragma mark - collectionView
- (void)createCollectionView{
    

    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
   
    self.flowLayout.itemSize = CGSizeMake(WIDTH  , HEIGHT1 - 113);
  //  self.flowLayout
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    [self.flowLayout release];
    
   self.flowLayout.scrollDirection =UICollectionViewScrollDirectionHorizontal ;
    
   

    //collection
    self.collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 104, WIDTH  , HEIGHT1 - 113) collectionViewLayout:self.flowLayout];
    
    [self.view addSubview:self.collectView];
    self.collectView.dataSource = self;
    self.collectView.delegate = self;
    
    
    self.collectView.pagingEnabled = YES;
    
    
    self.collectView.showsVerticalScrollIndicator = NO;
    [self.collectView registerClass:[CellForpicUrlCollectionViewCell class] forCellWithReuseIdentifier:@"poolForCell"];
    
    [self.collectView registerClass:[CellForKT class] forCellWithReuseIdentifier:@"poolForCell1"];
    [self.collectView registerClass:[CellForCT class] forCellWithReuseIdentifier:@"poolForCell2"];
    [self.collectView registerClass:[WSJ class] forCellWithReuseIdentifier:@"poolForCell3"];
    [self.collectView registerClass:[Other class] forCellWithReuseIdentifier:@"poolForCell4"];
    
    
    self.collectView.dk_backgroundColorPicker = DKColorWithRGB(0xF5F5FF, 0x343434);
    

    [self.collectView release];

}




#pragma mark - collection DataSourse
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arrayTitle.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.item == 0) {
        
        CellForpicUrlCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell" forIndexPath:indexPath];
        
        

        
        cell.delegate = self;
        
        return cell;
        
        
        
    }else if (indexPath.item == 1){
    
        CellForKT *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell1" forIndexPath:indexPath];
        cell.delegate = self;

        return cell;
    
    }else if (indexPath.item == 2){
        
        CellForCT *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell2" forIndexPath:indexPath];
        cell.delegate = self;

        return cell;
        
    }else if (indexPath.item == 3){
        
        WSJ *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell3" forIndexPath:indexPath];
        cell.delegate = self;

        
        return cell;
        
    }else {
        
        Other *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForCell4" forIndexPath:indexPath];
        cell.delegate = self;

        return cell;
        
    }

    


}


#pragma mark - CellForpicUrlCollectionViewCell的协议方法
- (void)didClickItem:(ModelImage *)model {
    
    ShowItem *show = [[ShowItem alloc]init];
    
    show.catid =  [model.catid integerValue];
    
    [self.navigationController pushViewController:show animated:YES];
    
    [show release];
}


#pragma mark - 知识点1 KVO(键值观察者)
/** 核心: 观察者 观察 某一个对象的属性变化 */
- (void)createKVOAction{
    
    //VC 观察 collection 的 collectionOffset 属性
    
    [self.collectView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"context"];
    
    
}
/** 当被观察者属性发生变化的时候,调用此方法 */
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    CGFloat x = [[change objectForKey:@"new"]CGPointValue].x;
    // CGFloat redX = x * WIDTH /5  /WIDTH;
    CGFloat redX = x  /5  ;
    
    // 修改 RedLine 位置
    self.viewForRedline.transform = CGAffineTransformMakeTranslation(redX, 0);
    
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
